package org.mch.func.m_px.yunsf;

import java.awt.event.ActionEvent;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;

public class AnWeiQuFan_Event extends FuncEvent {
	private static final long serialVersionUID = -589365474532268721L;
	private ToolBinaryFBY toolbinary = new ToolBinaryFBY();

	public AnWeiQuFan_Event(FuncPanel panel) {
		super(panel);
	}

	@Override
	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_js_erjinzhi()) {
			onJsErJinZhi();
		} else if (e.getSource() == getPanel().getBtn_int_qufan()) {
			onIntQuFan();
		} else if (e.getSource() == getPanel().getBtn_clear()) {
			getPanel().getTextArea().setText("");
		}

		else if (e.getSource() == getPanel().getBtn_erjinzhi_qufan()) {
			onErJinZhiQuFan();
		}

		else if (e.getSource() == getPanel().getBtn_BumaToFanma()) {
			String alltext = getPanel().getTextArea().getText();
			tool.checkNull(alltext, "请先进行二进制取反！");
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byBuMaToFanMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		} else if (e.getSource() == getPanel().getBtn_FanmaToBuma()) {
			String alltext = getPanel().getTextArea().getText();
			tool.checkNull(alltext, "请先进行二进制取反！");
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byFanMaToBuMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		}

		else if (e.getSource() == getPanel().getBtn_FanmaToYuanma()) {
			String alltext = getPanel().getTextArea().getText();
			tool.checkNull(alltext, "请先进行二进制取反！");
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byFanMaToYuanMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		} else if (e.getSource() == getPanel().getBtn_YuanmaToFanma()) {
			String alltext = getPanel().getTextArea().getText();
			tool.checkNull(alltext, "请先进行二进制取反！");
			String zhhang = alltext.substring(alltext.length() - 32, alltext.length());
			String s = toolbinary.byYuanMaToFanMa(zhhang);
			getPanel().getTextArea().insert("\n\n" + s, alltext.length());
		}
	}

	/**
	 * Java 取反运算符直接计算取反结果
	 */
	private void onIntQuFan() {
		int ii = Integer.parseInt(getPanel().getText_int().getValue());
		getPanel().getText_int_qufan().setValue(~ii);
	}

	/**
	 * 计算二进制
	 */
	private void onJsErJinZhi() {
		int ii = Integer.parseInt(getPanel().getText_int().getValue());

		if (ii > 0) {
			String ejz = Integer.toBinaryString(ii);
			StringBuilder sb_ejzma = new StringBuilder();

			for (int i = 0, len = 32 - ejz.length(); i < len; i++) {
				sb_ejzma.append("0");
			}
			sb_ejzma.append(ejz);

			getPanel().getText_erjinzhi_buma().setValue(sb_ejzma);// 补码
			getPanel().getText_erjinzhi_fanma().setValue(sb_ejzma);// 反码
			getPanel().getText_erjinzhi_yuanma().setValue(sb_ejzma);// 原码
		} else if (ii < 0) {
			String ejz = Integer.toBinaryString(ii);
			getPanel().getText_erjinzhi_buma().setValue(ejz);// 补码

			String fm = toolbinary.byBuMaToFanMa(ejz);
			getPanel().getText_erjinzhi_fanma().setValue(fm);// 反码

			String yuanma = toolbinary.byFanMaToYuanMa(fm);
			getPanel().getText_erjinzhi_yuanma().setValue(yuanma);// 原码

		} else {
			String ejz = Integer.toBinaryString(ii);
			getPanel().getText_erjinzhi_buma().setValue(ejz);// 补码
			getPanel().getText_erjinzhi_fanma().setValue(ejz);// 反码
			getPanel().getText_erjinzhi_yuanma().setValue(ejz);// 原码
		}
	}

	/**
	 * 二进制取反
	 */
	private void onErJinZhiQuFan() {
		String s_ejz = getPanel().getText_erjinzhi_buma().getValue();

		StringBuilder sb_qf = toolbinary.tool_ZeroToOneOneToZero(s_ejz);

		if (getPanel().getTextArea().getText().length() == 0) {
			getPanel().getTextArea().insert(sb_qf.toString(), getPanel().getTextArea().getText().length());
		} else {
			getPanel().getTextArea().insert("\n\n" + sb_qf.toString(), getPanel().getTextArea().getText().length());
		}
	}

	@Override
	public AnWeiQuFan_Panel getPanel() {
		return (AnWeiQuFan_Panel) super.getPanel();
	}
}
